package cn.edu.nwsuaf.examcenter.service.serviceImpl;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import cn.edu.nwsuaf.examcenter.model.PageModel;
import cn.edu.nwsuaf.examcenter.pojo.College;
import cn.edu.nwsuaf.examcenter.service.CollegeService;

@Service(value="collegeService")
public class CollegeServiceImpl extends BaseServiceImpl<College> implements CollegeService{
	
	public String addCollege(College college){
		super.saveData(college);
		return "success";
	}
	public String addCollegeList(List<College> list){
		super.saveDataList(list);
		return "success";
	}
	
	public String deleteCollege(String collegeNo){
		College college = super.getById(College.class, collegeNo);
		super.deleteData(college);
		return "success";
	}
	public String deleteCollegeList(List<String> list){
		List<College> collegeList = new ArrayList<>();
		for(String key:list){
			collegeList.add(super.getById(College.class, key));
		}
		super.deleteDataList(collegeList);
		
		return "success";
	}
	
	public List<College> jsonLoadCollege(){
		return super.findAll(College.class);
	}
	
	/****
	 * 
	 * @param key 关键字
	 * @param p 当前页码
	 * @param modelMap
	 */
	public void loadCollege(ModelMap modelMap){
		PageModel<College> collegeModel = new PageModel<>();
		List<College> list = super.findAll(College.class);
		collegeModel.setTotalrows(list.size());
		collegeModel.setList(list);
		modelMap.addAttribute("collegeModel", collegeModel);
	}
	/****
	 * 数据导出
	 * @param key
	 * @param request
	 * @param response
	 */
	public void collegeExport(String key, HttpServletRequest request, HttpServletResponse response){
	
		String[] param = {key};
		
		List<College> list = null;
		
		XSSFWorkbook workbook = null;
		XSSFSheet sheet = null;
		OutputStream os = null;
		
		/*配置response*/
		
		try {
			
			/*加载模板*/
			String fileName=new String("学院信息.xlsx".getBytes("UTF-8"),"iso-8859-1");
			os = response.getOutputStream();
			//xssfWorkbook = new XSSFWorkbook(is);
			//xssfSheet = xssfWorkbook.getSheetAt(0);
			
			/*response配置为文件下载*/
			response.reset(); 
			response.setContentType("application/octet-stream; charset=utf-8"); 
			response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
			
			/*查询数据*/
			if(key!=null){
				list = baseDao.findByHql("FROM College where collegeNo=?", param);
			}else{
				list = baseDao.findAll(College.class);
			}
			
			/*第一行*/
			String[] cellTitle = {"学院编号","学院名称"};
			
			/*创建*/
			workbook = new XSSFWorkbook();
			sheet = workbook.createSheet();
			workbook.setSheetName(0, "学院信息");
			
			XSSFFont font = workbook.createFont();  
			font.setColor(XSSFFont.COLOR_NORMAL);
			font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
			XSSFCellStyle cellStyle = workbook.createCellStyle();//创建格式
			cellStyle.setFont(font);
			cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
			
			/*创建标题行*/
			XSSFRow titleRow = sheet.createRow((short) 0);//第一行标题
			for(int i = 0,size = cellTitle.length; i < size; i++){//创建第1行标题单元格    
				switch(i){
				   	case 0:
				   		sheet.setColumnWidth(0, 3000);
				   		break;
				   	case 1:
				   		sheet.setColumnWidth(1,10000);
				   		break;
				}
				   
				XSSFCell cell = titleRow.createCell(i,0);        
				cell.setCellStyle(cellStyle);
				cell.setCellType(XSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(cellTitle[i]);
			}
			if(list!=null && !list.isEmpty()){
				XSSFCellStyle style = workbook.createCellStyle();
				for(int i = 0; i<list.size(); i++){
					College college = list.get(i);
					XSSFRow row = sheet.createRow((short)i+1);
					for(int j = 0; j<cellTitle.length; j++){
						XSSFCell cell = row.createCell(j, 0);// 在上面行索引0的位置创建单元格
				        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
				        switch (j) {
						case 0:
							cell.setCellValue(college.getCollegeNo());
							break;

						case 1:
							cell.setCellValue(college.getCoolegeName());
							break;
						}
					}
				}
			}
			workbook.write(os);
			os.flush();
			   	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
